//
//  BaiduRouteDetailTableViewController.m
//  WisdomZhuHai
//
//  Created by Ryan on 15-2-2.
//  Copyright (c) 2015年 wyd. All rights reserved.
//

#import "BaiduRouteDetailTableViewController.h"
#import "BaiduRouteTableViewController.h"
#import "Common.h"

@interface BaiduRouteDetailTableViewController ()

@end

@implementation BaiduRouteDetailTableViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, self.view.bounds.size.height - self.view.bounds.size.height / 3)];
    self.tableView.backgroundColor = [Common colorWithRGB:0xf7f7f7];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.view addSubview:_tableView];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


#pragma mark - Cell

- (void)routeCell:(UITableViewCell*)cell index:(NSInteger)index {
    NSString *instruction = nil;
    if (_is_bus) {
        BMKTransitStep* transitStep = [_busLine.steps objectAtIndex:index];
        instruction = transitStep.instruction;
    } else {
        BMKDrivingStep* transitStep = [_carLine.steps objectAtIndex:index];
        instruction = transitStep.instruction;
    }
//        BMKDrivingStep* transitStep = [_carLine.steps objectAtIndex:index];
    UIView *circleView = [[UIView alloc] initWithFrame:CGRectMake(20, 10, 40, 40)];
    UIImageView *circleImgView = [[UIImageView alloc] initWithFrame:CGRectMake(5, 5, circleView.bounds.size.width - 10, circleView.bounds.size.height - 10)];
    if (index % 2 == 0) {
        circleImgView.image = [UIImage imageNamed:@"map/blue_gray_circle@2x"];
    } else {
        circleImgView.image = [UIImage imageNamed:@"map/blue_circle@2x"];
    }
    UILabel *circleLab = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, circleView.bounds.size.width, circleView.bounds.size.height)];
    circleLab.font = [UIFont fontWithName:@"Arial" size:20.0];
    circleLab.textColor = [UIColor whiteColor];
    circleLab.textAlignment = UITextAlignmentCenter;
    circleLab.text = [NSString stringWithFormat:@"%d", index + 1];
    [circleView addSubview:circleImgView];
    [circleView addSubview:circleLab];
    
    UILabel *descLab = [[UILabel alloc] initWithFrame:CGRectMake(70, 0, self.view.bounds.size.width - 90, 60)];
    descLab.textAlignment = UITextAlignmentLeft;
    descLab.textColor = [Common colorWithRGB:0xcccccc];
    descLab.numberOfLines = 3;
    descLab.font = [UIFont fontWithName:@"Arial" size:15.0];
    NSString *temp = [self filterHTML:instruction];
    descLab.text = temp;
    [cell.contentView addSubview:circleView];
    [cell.contentView addSubview:descLab];
}

- (void)distanceTimeCell:(UITableViewCell*)cell {
    UIView *circleView = [[UIView alloc] initWithFrame:CGRectMake(20, 10, 40, 40)];
    UILabel *circleLab = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, circleView.bounds.size.width, circleView.bounds.size.height)];
    circleLab.textAlignment = UITextAlignmentCenter;
    circleLab.text = @"- -";
    [circleView addSubview:circleLab];
    
    UILabel *distance_time_lab = [[UILabel alloc] initWithFrame:CGRectMake(70, 10, self.view.bounds.size.width - 90, 40)];
    distance_time_lab.textAlignment = UITextAlignmentLeft;
    distance_time_lab.font = [UIFont fontWithName:@"Arial" size:15.0];
    distance_time_lab.textColor = [Common colorWithRGB:0xcccccc];
    float distance = 0.0;
    int hours = 0;
    int minutes = 0;
    if (_is_bus) {
        distance = _busLine.distance / 1000.0;
        hours = _busLine.duration.hours;
        minutes = _busLine.duration.minutes;
    } else {
        distance = _carLine.distance / 1000.0;
        hours = _carLine.duration.hours;
        minutes = _carLine.duration.minutes;
    }
    NSNumberFormatter *numberFormatter = [[[NSNumberFormatter alloc] init] autorelease];
    [numberFormatter setPositiveFormat:@"###,##0.00;"];
    NSString *distanceString = [numberFormatter stringFromNumber:[NSNumber numberWithDouble:distance]];
    distance_time_lab.text = [NSString stringWithFormat:@"%@公里，大约%d小时%d分钟", distanceString, hours, minutes];
    if (hours == 0) {
        distance_time_lab.text = [NSString stringWithFormat:@"%@公里，大约%d分钟", distanceString, minutes];
    }
    
    [cell addSubview:circleView];
    [cell addSubview:distance_time_lab];
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (_is_bus) {
        return [_busLine.steps count] + 1;
    } else {
        return [_carLine.steps count] + 1;
    }
    
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 50;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 40;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return 40;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, 40)];
    headerView.backgroundColor = [UIColor whiteColor];
    if (section == 0) {
        UILabel *meLab = [[UILabel alloc] initWithFrame:CGRectMake(25, 10, 100, 20)];
        meLab.text = @"我的位置";
        UIButton *dismiss = [[UIButton alloc] initWithFrame:CGRectMake(260, 10, 40, 20)];
        [dismiss setTitleColor:[Common colorWithRGB:0x18aaf2] forState:UIControlStateNormal];
        [dismiss setTitleColor:[Common colorWithRGB:0xcccccc] forState:UIControlStateHighlighted];
        [dismiss setTitle:@"返回" forState:UIControlStateNormal];
        [dismiss addTarget:self action:@selector(disMissView) forControlEvents:UIControlEventTouchUpInside];
        [headerView addSubview:meLab];
        [headerView addSubview:dismiss];
    }
    return headerView;
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    UIView *footerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, 40)];
    footerView.backgroundColor = [UIColor whiteColor];
    if (section == 0) {
        UILabel *meLab = [[UILabel alloc] initWithFrame:CGRectMake(25, 10, 200, 20)];
        meLab.text = _name;
        UIButton *dismiss = [[UIButton alloc] initWithFrame:CGRectMake(210, 10, 60, 20)];
        [dismiss setTitleColor:[Common colorWithRGB:0xff5000] forState:UIControlStateNormal];
        [dismiss setTitleColor:[Common colorWithRGB:0xcccccc] forState:UIControlStateHighlighted];
        dismiss.titleLabel.font = [UIFont systemFontOfSize: 15.0];
        [dismiss setTitle:@"进入地图" forState:UIControlStateNormal];
        [dismiss addTarget:self action:@selector(beginBaiduMapnavigate) forControlEvents:UIControlEventTouchUpInside];
        UIView *localView = [[UIView alloc] initWithFrame:CGRectMake(270, 0, 40, 40)];
        UIImageView *localImgView = [[UIImageView alloc] initWithFrame:CGRectMake( 12, 6, 15, 22)];
        localImgView.image = [UIImage imageNamed:@"map/icon_local"];
        UIImageView *shadowImgView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 25, localView.bounds.size.width, 10)];
        shadowImgView.image = [UIImage imageNamed:@"map/icon_shadow"];
        [localView addSubview:shadowImgView];
        [localView addSubview:localImgView];
        [footerView addSubview:meLab];
        [footerView addSubview:dismiss];
        [footerView addSubview:localView];
    }
    return footerView;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = nil;
    int count = 0;
    if (_is_bus) {
        count = [_busLine.steps count];
    } else {
        count = [_carLine.steps count];
    }
    if (indexPath.row == count) {
        cell = [tableView dequeueReusableCellWithIdentifier:@"DistanceTimeCell"];
        if(cell == nil)
        {
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"DistanceTimeCell"];
        } else {
            while ([cell.contentView.subviews lastObject] != nil) {
                [(UIView*)[cell.contentView.subviews lastObject] removeFromSuperview];  //删除并进行重新分配
            }
        }
        [self distanceTimeCell:cell];
    } else {
        cell = [tableView dequeueReusableCellWithIdentifier:@"RouteCell"];
        if(cell == nil)
        {
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"RouteCell"];
        } else {
            while ([cell.contentView.subviews lastObject] != nil) {
                [(UIView*)[cell.contentView.subviews lastObject] removeFromSuperview];  //删除并进行重新分配
            }
        }
        [self routeCell:cell index:indexPath.row];
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
}

#pragma mark - private

- (void)disMissView {
    [self dismissModalViewControllerAnimated:YES];
}

- (void)beginBaiduMapnavigate {
    [self dismissViewControllerAnimated:YES completion:^{
        [self.mapDelegate beginBaiduMapNav:_index];
    }];
}

-(NSString *)filterHTML:(NSString *)html
{
    NSScanner * scanner = [NSScanner scannerWithString:html];
    NSString * text = nil;
    while([scanner isAtEnd]==NO)
    {
        //找到标签的起始位置
        [scanner scanUpToString:@"<" intoString:nil];
        //找到标签的结束位置
        [scanner scanUpToString:@">" intoString:&text];
        //替换字符
        html = [html stringByReplacingOccurrencesOfString:[NSString stringWithFormat:@"%@>",text] withString:@""];
    }
    //    NSString * regEx = @"<([^>]*)>";
    //    html = [html stringByReplacingOccurrencesOfString:regEx withString:@""];
    return html;
}

@end
